/*
 * Copyright (c) 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.libermundi.theorcs.security.tapestry.services.impl;

import java.util.HashMap;
import java.util.Map;

import org.libermundi.theorcs.core.exceptions.EmailException;
import org.libermundi.theorcs.core.tapestry.services.AppHelper;
import org.libermundi.theorcs.core.tapestry.services.FreemarkerEmailService;
import org.libermundi.theorcs.security.SecurityConstants;
import org.libermundi.theorcs.security.model.User;
import org.libermundi.theorcs.security.services.UserManager;
import org.libermundi.theorcs.security.tapestry.pages.Activation;
import org.libermundi.theorcs.security.tapestry.services.UserServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Martin Papy
 *
 */
public class UserServicesImpl implements UserServices {
	private static final Logger logger = LoggerFactory.getLogger(UserServicesImpl.class);
	
	private final UserManager _userManager;
	
	private final AppHelper _appHelper;
	
	private final FreemarkerEmailService _emailService;
	
	public UserServicesImpl(UserManager userManager, AppHelper appHelper, FreemarkerEmailService emailService){
		this._userManager = userManager;
		this._appHelper = appHelper;
		this._emailService = emailService;
	}

	/* (non-Javadoc)
	 * @see org.libermundi.theorcs.security.tapestry.services.UserServices#sendActivationEmail(org.libermundi.theorcs.security.model.User)
	 */
	@Override
	public void sendActivationEmail(User user) {
		user.resetActivationKey();
		
		_userManager.save(user);
		
		Map<String, Object> parameters = new HashMap<>();
		
		parameters.put("login", user.getUsername());
		
		parameters.put("activationUrl", _appHelper.getEventLink(Activation.class, "activate",
					user.getUid(), user.getActivationKey()).toAbsoluteURI(Boolean.TRUE));
		parameters.put("siteUrl", _appHelper.getPageLink("index").toAbsoluteURI());
		
		try {
			_emailService.sendEmail(SecurityConstants.EMAIL_VALIDATION_ID, parameters, user);
		} catch (EmailException e) {
			logger.error("Could not send Activation Email to " + user, e);
		}

	}

	/* (non-Javadoc)
	 * @see org.libermundi.theorcs.security.tapestry.services.UserServices#activateUser(java.lang.String)
	 */
	@Override
	public boolean activateUser(String userUid, String activationKey) {
		User userToActivate = _userManager.getUserByUID(userUid,true);
		if(userToActivate == null){
			return Boolean.FALSE;
		}
		
		if(userToActivate.isDeleted() || !userToActivate.isEnabled()) {
			return Boolean.FALSE;
		}
		
		if(userToActivate.getActivationKey().equals(activationKey)){
			userToActivate.setActive(Boolean.TRUE);
			userToActivate.resetActivationKey();
			_userManager.save(userToActivate);
			return Boolean.TRUE;
		}

		return Boolean.FALSE;
	}

}
